package com.mgs.tagger.fsdata
{
	import flash.filesystem.File;

	public class DirectoryScanner
	{
		public function DirectoryScanner()
		{
		}
		
		public function scan(directory:File, matchExtensions:Vector.<String>, recursive:Boolean):Vector.<File>{
			var results:Vector.<File> = new Vector.<File>();
			internalScan(directory, matchExtensions, results, recursive);
			return results;
		}
		
		private static function internalScan(directory:File, matchExtensions:Vector.<String>, results:Vector.<File>, recursive:Boolean):void{
			for each(var content:File in directory.getDirectoryListing()){
				if(!content.isDirectory){
					for each(var ext:String in matchExtensions){
						if(endWith(content.name, ext)){
							results.push(content);
							break;
						}
					}
				}else if(recursive){
					internalScan(content, matchExtensions, results, recursive);
				}
			}
		}
		
		private static function endWith(str:String, end:String):Boolean{
			var index:int = str.toLowerCase().indexOf(end.toLowerCase());
			return index >= 0 && index == str.length - end.length;
		}
	}
}